from django.db import models

from django.core.exceptions import ValidationError  # 为字段添加正则
import re

# 菜单栏
class Select_Menu(models.Model):
    id = models.AutoField(primary_key=True)     # 菜单栏的序号
    menu_name = models.CharField(max_length=20)     # 菜单栏显示名称
    menu_html_id = models.CharField(max_length=20)  # 网页显示id名称
    menu_url = models.CharField(max_length=30)     # 菜单栏路径

    def __str__(self):
        return self.menu_name

    class Meta:
        ordering = ["menu_html_id"]
        verbose_name = "菜单栏"
        verbose_name_plural = "菜单栏"


# 学院班级
class College_Class(models.Model):
    id = models.AutoField(primary_key=True)  # 学院班级序号
    name = models.CharField(max_length=64)  # 学院班级名称

    def __str__(self):
        return self.name

    class Meta:
        ordering = ["-id"]
        verbose_name = "学院班级"
        verbose_name_plural = "学院班级"


# 阶段课程
class Course(models.Model):
    id = models.AutoField(primary_key=True)  # 阶段课程序号
    name = models.CharField(max_length=64)  # 阶段课程名称, 例如：机器学习2
    detail = models.TextField()  # 阶段课程备注信息
    college_class = models.OneToOneField('College_Class', on_delete=models.CASCADE, default=1)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ["-id"]
        verbose_name = "阶段课程"
        verbose_name_plural = "阶段课程"


# 用户信息
class UserInfo(models.Model):
    gender = (
        ('male', "男"),
        ('female', "女"),
    )

    user_levels = (
        ('1', '超级用户'),
        ('2', '班级管理'),
        ('3', '普通用户')
    )

    id = models.AutoField(primary_key=True)     # 用户序号
    name = models.CharField(max_length=64, unique=True, null=False, verbose_name='姓名')     # 姓名
    user_level = models.CharField(max_length=32, choices=user_levels, default='普通用户', verbose_name='用户等级')  # 用户等级
    username = models.CharField(max_length=64, unique=True, null=False, verbose_name='用户名')     # 用户名
    password = models.CharField(max_length=64, verbose_name='密码')      # 密码
    classs = models.ForeignKey('College_Class', on_delete=models.CASCADE, verbose_name='所在班级', default=1)       # 所在班级
    course = models.ForeignKey('Course', on_delete=models.CASCADE, verbose_name='所选课程', default=1)         # 所选课程
    head_image = models.ImageField(upload_to="head_image", verbose_name='头像地址')    # 头像地址
    sex = models.CharField(max_length=32, choices=gender, default='男', verbose_name='性别')      # 性别
    reg_time = models.DateTimeField(auto_now_add=True, verbose_name='注册时间')      # 注册时间
    last_login = models.DateTimeField(auto_now=True, verbose_name='最后登陆时间')    # 最后登陆时间
    email = models.EmailField()
    error_lilun_titles = models.ManyToManyField('Lilun_title')

    def __str__(self):
        return self.name

    class Meta:
        ordering = ["-id"]
        verbose_name = "用户"
        verbose_name_plural = "用户"

#  --------------------------------------------------题库question_bank--------------------------------------------------------------
# 理论题目
class Lilun_title(models.Model):
    id = models.AutoField(primary_key=True)  # 用户序号

    zhishidian = models.CharField(max_length=20, verbose_name='知识点')   # 知识点
    leixing =   models.CharField(max_length=20, verbose_name='类型')      # 类型
    tigan =     models.CharField(max_length=200, verbose_name='题干')     # 题干
    xxa =       models.TextField(verbose_name='选项A')  # 选项A
    xxb =       models.TextField(verbose_name='选项B')  # 选项B
    xxc =       models.TextField(verbose_name='选项C')  # 选项C
    xxd =       models.TextField(verbose_name='选项D')  # 选项D
    xxe =       models.TextField(verbose_name='选项E')  # 选项E
    xxf =       models.TextField(verbose_name='选项F')  # 选项F
    daan =      models.CharField(max_length=100, verbose_name='答案')     # 答案
    jiexi =     models.CharField(max_length=100, verbose_name='解析')     # 解析
    chutiren =  models.CharField(max_length=100, verbose_name='出题人')    # 出题人

    kaoti = models.ForeignKey('Lilun_unit_name',on_delete=models.CASCADE)
    bianhao = models.IntegerField()

    def __str__(self):
        return self.tigan

    class Meta:
        ordering = ["id"]
        verbose_name = "理论试题"
        verbose_name_plural = "理论试题"


# 理论试卷名称
class Lilun_unit_name(models.Model):
    id = models.AutoField(primary_key=True)     # 用户序号
    name = models.CharField(max_length=100)     # 理论试卷名称
    course = models.ForeignKey('Course', on_delete=models.CASCADE, default=1)  # 所属课程  -- 20190306 - 110

    def __str__(self):
        return self.name

    class Meta:
        ordering = ["-id"]
        verbose_name = "理论试卷名称"
        verbose_name_plural = "理论试卷名称"


# 技能题目
class Jineng_title(models.Model):
    id = models.AutoField(primary_key=True)  # 用户序号
    theme = models.CharField(max_length=64)     # 题目主题， 简明主题（一般不超过10个字符
    tigan = models.TextField()      # 技能题目内容
    code = models.TextField()       # 技能代码
    codedata = models.FileField(upload_to="jineng_data_file", verbose_name='数据文件地址', null=True, blank=True)   # 技能数据
    codedata_enlabel = models.CharField(max_length=32, default='test')      # 技能数据文件英文名称  ， 用于兼容 windows server iis 不支持中文路径
    detail = models.CharField(max_length=64)    # 技能题目备注
    kaoti = models.ForeignKey('Jineng_unit_title', on_delete=models.CASCADE)    # 所属单元

    def __str__(self):
        return self.tigan

    class Meta:
        ordering = ["id"]
        verbose_name = "技能题目"
        verbose_name_plural = "技能题目"


# 技能试卷名称（技能单元名称, 注意和理论名称的区别）
class Jineng_unit_title(models.Model):
    id = models.AutoField(primary_key=True)     # 技能题目序号
    name = models.CharField(max_length=128)     # 技能单元名称， 固定格式：课程名_单元名
    course = models.ForeignKey('Course', on_delete=models.CASCADE)      # 所属课程

    def __str__(self):
        return self.name

    class Meta:
        ordering = ["-id"]
        verbose_name = "技能试卷名称"
        verbose_name_plural = "技能试卷名称"

# ——————————————————好玩的项目————————————————————————————
#  interesting 好玩的项目
class Interesting_Menu(models.Model):
    id = models.AutoField(primary_key=True)  # 菜单栏的序号
    interesting_menu_name = models.CharField(max_length=20)  # 菜单栏显示名称
    interesting_menu_html_id = models.CharField(max_length=20)  # 网页显示id名称
    interesting_menu_url = models.CharField(max_length=30)  # 菜单栏路径

    def __str__(self):
        return self.interesting_menu_name

    class Meta:
        ordering = ["id"]
        verbose_name = "好玩菜单栏"
        verbose_name_plural = "好玩菜单栏"

